package com.jlastu.znb.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.google.android.material.imageview.ShapeableImageView;
import com.jlastu.znb.R;
import com.jlastu.znb.constan.Constance;
import com.jlastu.znb.entity.ShopUserData;
import com.jlastu.znb.inter.OnFollowClickListerner;

import java.util.ArrayList;
import java.util.List;

public class FollowAdapter extends RecyclerView.Adapter<FollowAdapter.ViewHolder> {

   private List<ShopUserData> mList = new ArrayList<>();
   private Context mContext;
   private String page;

   public FollowAdapter(Context context, String page) {
      this.mContext = context;
      this.page = page;
   }

   public void setData(List<ShopUserData> list) {
      mList.clear();
      mList.addAll(list);
      notifyDataSetChanged();
   }
   public void addData(List<ShopUserData> shopUserData) {
      mList.addAll(shopUserData);
      notifyDataSetChanged();
   }
   public void addData(ShopUserData shopUserData) {
      mList.add(shopUserData);
      notifyDataSetChanged();
   }
   public void addData(ShopUserData shopUserData,int position) {
      mList.add(position,shopUserData);
      notifyDataSetChanged();
   }
   @NonNull
   @Override
   public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
      View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.follow_shop_item, parent, false);
      return new ViewHolder(view);
   }

   @Override
   public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
      ShopUserData shopUserData = mList.get(position);
      Glide.with(holder.itemView).load(shopUserData.getIconImgUrl()).into(holder.userIcon);
      Glide.with(holder.itemView).load(shopUserData.getImgUrl()).into(holder.imgBig);
      holder.tvComment.setText(shopUserData.getCommentCount() +"");
      holder.zanCount.setText(shopUserData.getZanCount() +"");
      holder.tvTime.setText(shopUserData.getTime());
      holder.tvUserName.setText(shopUserData.getUserName());
      holder.tvTitle.setText(shopUserData.getTitle());
      if(Constance.FOLLOW_PAGE.equals(page)) {
         holder.followed.setVisibility(View.GONE);
         holder.follow.setVisibility(View.GONE);
      }else if(shopUserData.isFollow()){
         holder.follow.setVisibility(View.GONE);
         holder.followed.setVisibility(View.VISIBLE);
      }else {
         holder.followed.setVisibility(View.GONE);
         holder.follow.setVisibility(View.VISIBLE);
      }
      if("-1".equals(shopUserData.getUserId())) {
         holder.followed.setVisibility(View.GONE);
         holder.follow.setVisibility(View.GONE);
      }
      holder.follow.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            shopUserData.setFollow(!shopUserData.isFollow());
            notifyItemChanged(position);
            onFollowClickListerner.onFollowClick(shopUserData,position);
         }
      });
      holder.inShop.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            onFollowClickListerner.onInShop(shopUserData,position);
         }
      });

   }
   OnFollowClickListerner onFollowClickListerner;
   public void setOnFollowClickListerner(OnFollowClickListerner onFollowClickListerner) {
      this.onFollowClickListerner = onFollowClickListerner;
   }
   @Override
   public int getItemCount() {
      return mList != null ? mList.size() : 0;
   }

   public class ViewHolder extends RecyclerView.ViewHolder {
      ShapeableImageView userIcon,imgBig;
      TextView tvUserName,tvTime,tvComment,tvTitle,zanCount,followed,inShop,follow;

      public ViewHolder(@NonNull View itemView) {
         super(itemView);
         userIcon = itemView.findViewById(R.id.user_icon);
         tvUserName = itemView.findViewById(R.id.tv_name);
         tvTime = itemView.findViewById(R.id.tv_time);
         tvTitle = itemView.findViewById(R.id.tv_title);
         imgBig = itemView.findViewById(R.id.img_big);
         zanCount = itemView.findViewById(R.id.zan_count);

         tvComment = itemView.findViewById(R.id.comment_count);
         followed = itemView.findViewById(R.id.followed);
         inShop = itemView.findViewById(R.id.in_shop);
         follow = itemView.findViewById(R.id.follow);
      }
   }
}